//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;
using Eas.WebCrawler.PlugIns.Selectors;

namespace Eas.WebCrawler.PlugIns.Selectors {
    /// <summary>Specifies that all requests above a specific depth should not be followed, otherwise use the inner selector.</summary>
    public class MaximumDepthSelector : Eas.WebCrawler.PlugIns.Selectors.DecoratingSelector {
        private int _MaximumDepth;

        /// <summary>Constructs a <see cref="MaximumDepthSelector"/> out of a depth and an inner selector.</summary>
        /// <param name="maximumDepth">The maximum depth for requests.</param>
        /// <param name="innerSelector">The inner selector to use if the dpeth is less than the maximum.</param>
        public MaximumDepthSelector(int maximumDepth, LinkSelector innerSelector)
            : base(innerSelector) {
            _MaximumDepth = maximumDepth;
        }

        /// <summary>The maximum allowed dpeth of requests.</summary>
        /// <remarks>Root requests have a depth of <code>0</code>.</remarks>
        public int MaximumDepth {
            get { return _MaximumDepth; }
        }

        #region LinkSelector Members

        /// <summary>Determines whether or not a <see cref="Request"/> (link) should be crawled.</summary>
        /// <param name="request">The request which has just been encountered.</param>
        /// <returns><code>true</code> if the request should be crawled, otherwise <code>false</code>.</returns>
        /// <remarks>This implementation just delegates to its inner selector.  This is not broadly useful and so derived classes will likely want to override this function.</remarks>
        public override bool ShouldFollow(Request request) {
            if (request.Depth > _MaximumDepth)
                return false;
            return base.ShouldFollow(request);
        }
        #endregion

    }

}
